package threadStudy;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ShutDown {
    public static void main(String[] args) throws InterruptedException {
        // shutdown 初始化整个线程池关闭过程 仅仅新的任务不会增加了
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        for (int i = 0; i <= 1000; i++) {
            executorService.execute(new Task());
        }

        Thread.sleep(1500);
        // shutdown 已经添加的任务会继续执行 shutdown后面的任务不会执行
        executorService.shutdown();
        executorService.execute(new Task());

        // isShutdown() 是否进入了停止提交任务的状态
        boolean isShutdown = executorService.isShutdown();
        // 判断线程是否已经完全终止了
        boolean terminated = executorService.isTerminated();
        // 3秒内检测线程是否处于关闭状态
        boolean awaitTermination = executorService.awaitTermination(3, TimeUnit.SECONDS);
        // 立刻关闭线程池所有的线程 正在执行的线程中断信号，没有执行的线程会返回一个List
        List<Runnable> runnableList = executorService.shutdownNow();
        // runnableList已经在 队列中未执行的任务
    }
}
